import 'dart:developer';
import 'package:get/get.dart';
import 'package:ball_lion/entity/home/subs_list_entity.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/server/api/pay_api.dart';
import 'package:ball_lion/entity/pay/recharge_entity.dart';
import 'package:ball_lion/server/api/coupon_api.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/base/wqs_dialog.dart';
import 'package:ball_lion/module/pay/pay_bottom_sheet.dart';

class SubsDetailLogic extends GetxController {
  int _priceIndex = 0;
  int? roomId = Get.parameters['roomId'].toInt();
  List<String> benefitIcons = [
    "subs_icon_1.png",
    "subs_icon_2.png",
    "subs_icon_3.png"
  ];
  List<String> benefitNames = ["精选比赛", "方案免费", "超高性价比"];
  List<String> benefits = ["热门比赛优质解析", "每日足篮方案免费看", "每月最低可省888球币"];
  String? tip;
  SubsListEntity? entity;
  int get priceIndex => _priceIndex;

  set priceIndex(int value) {
    _priceIndex = value;
    update();
  }

  @override
  void onReady() {
    requestData();
    super.onReady();
  }

  Future toPay(String price, int type) async {
    Utils.onEvent('zjdy_ljzf');
    var x = double.parse(price.split(RegExp(r"[\u4e00-\u9fa5]")).join());
    double money = x;
    final coupons = await CouponApi.use1Coupons(
        gold: money, scene: PayScene.subs, otherId: '', roomId: roomId);
    final PayResult? result = await Get.bottomSheet(
        isScrollControlled: true,
        PayBottomSheet(
            payInfo: PayInfo(
                price: money.toString(),
                payScene: PayScene.subs,
                buyId: "${entity?.expertId ?? " "}-$type",
                ownerId: entity?.expertId ?? " ",
                coupons: coupons)));
    if (result != null && result.toPay == PayStatus.goldPay) {
      log("${result.toPay}");
      await PayApi.subsOrder(money, entity?.expertId ?? "",
          seq: DateTime.now().microsecondsSinceEpoch.toString(),
          subsType: type,
          couponId: result.coupon?.id,
          couponGold: result.coupon?.gold,
          roomId: roomId);
      Utils.onEvent('pay_done');
      if (Get.previousRoute == Routes.userPage) {
        Get.back();
      } else {
        Get.offAndToNamed(Routes.userPage,
            arguments: entity?.expertId,
            parameters: {"type": "2", "index": "dingyue"});
      }
    } else if (result?.toPay == PayStatus.directPay) {
      Utils.onEvent('dyxq_zfyd_qrzf');
      if (Get.previousRoute == Routes.userPage) {
        Get.back();
      } else {
        Get.offAndToNamed(Routes.userPage,
            arguments: entity?.expertId,
            parameters: {"type": "2", "index": "dingyue"});
      }
    } else if (result?.toPay == PayStatus.subcardExsit) {
      Get.dialog(
          const QsDialog.single("你还有未使用的订阅次数，全部使用后方可续订", confirmText: "知道了"));
    } else {
      Utils.onEvent('dyxq_zfyd_qxzf');
      Toast.text("购买失败");
    }
    User.fetchUserInfo(fetchFocus: false);
  }

  Future requestData() async {
    entity = await ExpertApi.getSubsDetail(Get.arguments, roomId: roomId);
    if (entity?.subsPrices?[2].min != null) {
      benefits[2] = "每月${entity?.subsPrices?[2].min}";
    }
    var tips = await OtherApi.getAppList("app_res_tips");
    try {
      tip =
          tips?.firstWhere((element) => element.title == "subs_detail").content;
    } catch (e) {
      log("no subs_detail");
    }
    update();
  }
}
